package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.CmsCompanyDO;
import com.bootdo.wx.service.CmsCompanyService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.common.utils.StringUtils;
/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-07-15 10:57:19
 */
 
@Controller
@RequestMapping("/wx/cmsCompany")
public class CmsCompanyController {
	@Autowired
	private CmsCompanyService cmsCompanyService;
	
	@GetMapping()
	@RequiresPermissions("wx:cmsCompany:cmsCompany")
	String CmsCompany(){
	    return "wx/cmsCompany/cmsCompany";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:cmsCompany:cmsCompany")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<CmsCompanyDO> cmsCompanyList = cmsCompanyService.likeList(query);
            int total = cmsCompanyList.size();
            PageUtils pageUtils = new PageUtils(cmsCompanyList, total);
            return pageUtils;
		}

		List<CmsCompanyDO> cmsCompanyList = cmsCompanyService.list(query);
		int total = cmsCompanyService.count(query);
		PageUtils pageUtils = new PageUtils(cmsCompanyList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:cmsCompany:add")
	String add(){
	    return "wx/cmsCompany/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:cmsCompany:edit")
	String edit(@PathVariable("id") String id,Model model){
		CmsCompanyDO cmsCompany = cmsCompanyService.get(id);
		model.addAttribute("cmsCompany", cmsCompany);
	    return "wx/cmsCompany/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:cmsCompany:add")
	public R save( CmsCompanyDO cmsCompany){
		Map<String, Object> map = new HashMap<>();
		map.put("delFlag", "0");
		List<CmsCompanyDO> list = cmsCompanyService.list(map);
		if(list.size()>0){
			return R.error("只能添加一个");
		}
	    cmsCompany.setId(UuidUtil.get32UUID());
	    cmsCompany.setDelFlag(0);
	    cmsCompany.setCaAt(new Date());
		if(cmsCompanyService.save(cmsCompany)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:cmsCompany:edit")
	public R update( CmsCompanyDO cmsCompany){
		cmsCompany.setUpAt(new Date());
		cmsCompanyService.update(cmsCompany);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:cmsCompany:remove")
	public R remove( String id){
		CmsCompanyDO cmsCompany = cmsCompanyService.get(id);
		cmsCompany.setDelFlag(1);
		cmsCompany.setUpAt(new Date());
		if(cmsCompanyService.update(cmsCompany)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:cmsCompany:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				CmsCompanyDO cmsCompany = cmsCompanyService.get(string);
				cmsCompany.setDelFlag(1);
				cmsCompany.setUpAt(new Date());
				cmsCompanyService.update(cmsCompany);
			}
		}
	
		return R.ok();
	}
	
}
